package sort.memory;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.function.Consumer;

public class SortUtil {

	public static void swap(int[] a, int x, int y){
		int temp = a[x];
		a[x] = a[y];
		a[y] = temp;
	}

	public static int[] newArray(int length) {
		int a[] = new int[length];
		Random random = new Random();
		for(int i = 0; i < length; i++){
			a[i] = random.nextInt(length + 1);
		}
		return a;
	}

	public static ArrayList<Integer> newList(int length) {
		ArrayList<Integer> a = new ArrayList<>(length);
		Random random = new Random();
		for(int i = 0; i < length; i++){
			a.add(random.nextInt(10));
		}
		return a;
	}

	public static ListNode toLinked(int[] array) {
		if (array.length == 0) {
			return null;
		}

		ListNode head = new ListNode(array[0]);
		ListNode node = head;
		for (int i = 1; i < array.length; i++) {
			node.next = new ListNode(array[i]);
			node = node.next;
		}
		return head;
	}

	public static void print(ListNode head) {
		while (head != null) {
			System.out.print(head.val + " ");
			head = head.next;
		}
		System.out.println();
	}

	public static void sortAndPrint(int a[], String printTag, Consumer<int[]> consumer) {
		int[] temp = a.clone();
		long start = System.currentTimeMillis();
		consumer.accept(temp);
		long end = System.currentTimeMillis();
		System.out.println(printTag + (end - start) / 1000.0);
//		System.out.println(Arrays.toString(temp));
	}
}
